const http = require('http')
const request = require('request')
const cheerio = require('cheerio')
const movieList = require('./data.js'); 
function MovieList2HTML(data) {
    return `<!DOCTYPE html>
        <html lang="en">
        <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style type="text/css">
            .gridtable {
                font-family: verdana,arial,sans-serif;
                font-size:11px;
                color:#333333;
                border-width: 1px;
                border-color: #666666;
                border-collapse: collapse;
            }
            .gridtable th {
                border-width: 1px;
                padding: 8px;
                border-style: solid;
                border-color: #666666;
                background-color: #dedede;
            }
            .gridtable td {
                border-width: 1px;
                padding: 8px;
                border-style: solid;
                border-color: #666666;
                background-color: #ffffff;
            }
        </style>
        </head>
        <body>
        <table width="500px" id="tb" align="center" class="gridtable">
            <th>电影id</th>
            <th>电影名称</th>
            <th>电影评分</th>
        </table>
        </body>
        <script>
            var movieList = ${JSON.stringify(data)};
            var tb = document.getElementById("tb");
            for(var i = 0; i < movieList.length; i++) {
                var tdNode = 
                    "<td>" +
                    movieList[i].movieId +
                    "</td>" +
                    "<td>" +
                    movieList[i].movieTitle +
                    "</td>" +
                    "<td>"  +
                    movieList[i].movieDetail + 
                    "</td>"
                var trNode = document.createElement("tr");
                trNode.innerHTML = tdNode;
                tb.appendChild(trNode);
            }
        </script>
        </html>`
}

http.createServer((request, response) => {
    console.log(request.method + " " + request.url)
    if (request.url == '/') {
        //获取正在热映电影数据
        movieList
            .then((data) => {
                console.log(data);
                response.setHeader('Content-Type','text/html');
                response.end(MovieList2HTML(data))
            })
        
    }
    else if (request.url == '/getlist') {
        response.setHeader('Content-Type','application/json');
        //获取正在热映电影数据
        movieList
            .then((data) => {
                response.end(JSON.stringify(data))
            })
    }
    else {
        response.end();
    }
    
}).listen(8080)

function del(data){
    var list = [];
    for(var i=0;i<data.length;i++){
        if(data[i].movieRange=="暂无评分"){

        }else{
            list.push(data[i])
        }
    }
    return list;
}
  
function sort(data){
    var sortList = del(data);
    var temp = new Object();
        for(var i=0;i<sortList.length;i++){
        for(var j=0;j<sortList.length;j++){
            if(sortList[i].movieRange>sortList[j].movieRange){
                temp=sortList[i];
                sortList[i]=sortList[j];
                sortList[j]=temp;
            }
        }
    }
    return sortList;
}



